package com.ctf.soft.ms.core;

import com.ctf.soft.ms.dao.dto.manage.LoginUser;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 名称： SpringInterceptor<br>
 * 描述： 请求过滤<br>
 * 最近修改时间：2015/7/30 14:03<br>
 * 修改说明：
 *
 * @author jiajl
 */
public class SpringInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object obj) throws Exception {
        String[] noFilters = new String[]{"skin", "scripts", "attached", "login", "verify"};
        String host = request.getRemoteHost();
        String uri = request.getRequestURI();
        System.out.println(host + "--->>>" + uri);
        boolean beFilter = true;
        for (String str : noFilters) {
            if (uri.indexOf(str) != -1) {
                beFilter = false;
                break;
            }
        }
        if (beFilter) {
            LoginUser user = (LoginUser) request.getSession().getAttribute(
                    "UserSession");
            if (user == null) {
                String path = request.getContextPath();
                response.sendRedirect(path + "/expired.jsp");
                return false;
            }
        }
        return true;
    }

    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object obj, ModelAndView mav)
            throws Exception {
    }

    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object obj, Exception ex)
            throws Exception {
    }
}
